//
//  YSPCommentModel.swift
//  GZWeibo007
//
//  Created by 朴裕识 on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class YSPCommentModel: NSObject {
    
    
    ///评论创建时间
    var created_at: String?
    ///评论的内容
    var text: String?
    ///评论的id
    var id: Int = 0
    ///评论者的信息
    var user: CZUser?
    
    ///字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String: AnyObject] {
                let userModel = CZUser(dict: dict)
                self.user = userModel
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    // 打印
    override var description: String {
        let prop = ["created_at", "text", "id", "user"]
        
        let dict = dictionaryWithValuesForKeys(prop)
        return "\n\t\t评论模型:\(dict)"
    }
    
    //MARK: - 获取评论
    class func getNewComment(since_id: Int, max_id: Int,id: Int, finished: (result: [YSPCommentModel]?, error: NSError?) -> ()) {
        
        CZNetworkTool.sharedInstance.getNewComment(since_id, max_id: max_id, id: id) { (result, error) -> () in
            if error != nil || result == nil {
                finished(result: nil, error: error)
                return
            }
            //来到这里说明有数据
            let resultArray = result!["comments"] as! [[String: AnyObject]]
            var resultModelArray = [YSPCommentModel]()
            for dict in resultArray {
                let commentModel = YSPCommentModel.init(dict: dict)
                resultModelArray.append(commentModel)
            }
            //将模型返回给调用者
            finished(result: resultModelArray, error: nil)
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    


}
